package basicelement;

import istar.agent.IstarJadeAgent;
import istar.behaviour.AbstractIstarBehaviour;
import istar.impl.AbstractBasicResource;
import jade.core.behaviours.Behaviour;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Date;



public class CurrentPosition extends AbstractBasicResource {

	private boolean log = true;
	private MarketPosition currentPosition = null;
		
		public CurrentPosition() {
			super("CurrentPosition"); 
		}

		@Override
		public Behaviour getBehaviour() {
			return new myBehaviour(this);
		}
		
		private class myBehaviour extends AbstractIstarBehaviour{

			AbstractBasicResource resource;
			private int estimatedTime = 100;
			
			public myBehaviour(AbstractBasicResource _resource){
				super(_resource);
			}

			
			@Override
			public long getEstimatedWaitingTime() {
				return estimatedTime;
			}

			@Override
			public void myAction() {
				try {
					
					BufferedReader in = new BufferedReader(new FileReader(((IstarJadeAgent)myAgent).configurationPath));
					String str, stockFile=null, moneyFile=null;
					String lowStockName = null, highStockName = null;
				    Double lowStockPriceOrdered = null, highStockPriceOrdered = null;
				    Double money = null, blocked = null;
					Integer qttLowStockOption=0 , qttHighStockOption=0;
					
				    while ((str = in.readLine()) != null) {
				        if (str.startsWith("StockFile=")){
				        	stockFile = str.substring(10);
				        }
				        if (str.startsWith("MoneyFile=")){
				        	moneyFile = str.substring(10);
				        }
				    }
				    in.close();

				    
				    if (stockFile!=null){
					    
					    BufferedReader bfStck = new BufferedReader(new FileReader(stockFile));
					    String line = bfStck.readLine();
					    int firstSeparator = line.indexOf(";", 0);
					    int secondSeparator = line.indexOf(";", firstSeparator+1);
					    
					    lowStockName = line.substring(firstSeparator+1, secondSeparator);
					    highStockName = line.substring(secondSeparator+1);
					    
					    String str2;
					    while ((str2 = bfStck.readLine())!=null){
					    	line = str2;
					    }
					    bfStck.close();

					    firstSeparator = line.indexOf(";", 0);
					    secondSeparator = line.indexOf(";", firstSeparator+1);
					    int thirdSeparator = line.indexOf(";", secondSeparator+1);
					    int fourthSeparator = line.indexOf(";", thirdSeparator+1);

					    qttLowStockOption = new Integer(line.substring(firstSeparator+1, secondSeparator));
					    qttHighStockOption = new Integer(line.substring(secondSeparator+1, thirdSeparator));

					    try {
					    	lowStockPriceOrdered = new Double(line.substring(thirdSeparator+1, fourthSeparator));
						} catch (Exception e) {
							e.printStackTrace();
						}
						
						try {
							highStockPriceOrdered = new Double(line.substring(fourthSeparator+1));
						} catch (Exception e) {
							e.printStackTrace();
						}
					    
				    }

				    if (moneyFile!=null){
				    	//File f = (new File(moneyFile));
				    	
						BufferedReader m = new BufferedReader(new FileReader(moneyFile));
						String header = m.readLine();
						
						String line ="";
					    String str2;
					    while ((str2 = m.readLine())!=null){
					    	line = str2;
					    }

					    m.close();

					    int firstSeparator = line.indexOf(";", 0);
					    int secondSeparator = line.indexOf(";", firstSeparator+1);
					    int thirdSeparator = line.indexOf(";", secondSeparator+1);
					    String sMoney = line.substring(firstSeparator+1, secondSeparator);
					    String sBlocked= line.substring(secondSeparator+1, thirdSeparator);
						money = new Double(sMoney);
						blocked = new Double (sBlocked);
				    }
				    
				    String sInstant = (String)((IstarJadeAgent)myAgent).getKnowledgeElement(getElement().getExecutionNumber(),"SimulationTime");
				    
				    currentPosition = new MarketPosition(sInstant, lowStockName, highStockName, money, blocked, qttLowStockOption, qttHighStockOption, lowStockPriceOrdered, highStockPriceOrdered);
				    System.out.println("========");
				    System.out.println("CurrentPosition: "+currentPosition.toString());
				    System.out.println("========");
				    
				    ((IstarJadeAgent)myAgent).setKnowledgeObject(getElement().getExecutionNumber(), "CurrentPosition", currentPosition);
				    
				    
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			
			public void checkConsistency() throws Exception {
				if (currentPosition.getQttLowStockOption()>0)
					throw new Exception("Invalid " + currentPosition.getLowStockOptionName() + " quantity:"+ currentPosition.getQttLowStockOption());
				
				if (currentPosition.getQttHighStockOption()<0)
					throw new Exception("Invalid " + currentPosition.getHighStockOptionName() + " quantity:"+ currentPosition.getQttHighStockOption());

				if (currentPosition.getPosition()==MarketPosition.SOLD){
					if (currentPosition.getQttLowStockOption()!= -currentPosition.getQttHighStockOption())
						throw new Exception("Invalid sold position: " + currentPosition.getLowStockOptionName() + "="+ currentPosition.getQttLowStockOption() +
																		currentPosition.getHighStockOptionName() + "="+ currentPosition.getQttHighStockOption());
					Double spread = (Double)((IstarJadeAgent)myAgent).getKnowledgeElement(getElement().getExecutionNumber(), "Spread");
					if (currentPosition.getQttLowStockOption()*spread != currentPosition.getBlocked())
						throw new Exception("Invalid sold position: " + currentPosition.getQttLowStockOption() + "*" + spread +
																		"!="+ currentPosition.getBlocked());
				}
			}
			
			@Override
			public int myOnEnd() {
				// TODO Auto-generated method stub
				return 1;
			}

			@Override
			public boolean done() {
				return true;
			}
		}
}

